package com.example.floristshop;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.floristshop.database.Flower;
import com.example.floristshop.database.Record;
import com.example.floristshop.database.User;
import com.example.floristshop.utils.DBFindUtil;
import com.example.floristshop.utils.TimeUtil;

import org.litepal.LitePal;

public class RecordInfoActivity extends AppCompatActivity {
    Toolbar toolbar;
    Button submit_btn;
    LinearLayout flower_layout;
    TextView time_tv, flower_name_tv, user_name_tv;
    ImageView flower_image_iv;
    EditText record_number_et, record_money_et;

    long timestamp;
    Record record;
    Flower flower;
    User user;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_info);

        // init data
        timestamp = getIntent().getLongExtra("record_time", 1633821577);
        record = DBFindUtil.findRecordByTimestamp(timestamp);
        flower = DBFindUtil.findFlowerByFlowerId(record.getFlower_id());
        user = DBFindUtil.findUserByUserId(record.getUser_id());

        // get view
        toolbar = (Toolbar)findViewById(R.id.modify_record_toolbar);
        submit_btn = (Button)findViewById(R.id.modify_record_submit_button);
        flower_layout = (LinearLayout)findViewById(R.id.modify_record_flower_info);
        time_tv = (TextView)findViewById(R.id.modify_record_time);
        flower_name_tv = (TextView)findViewById(R.id.modify_record_flower_name);
        user_name_tv = (TextView)findViewById(R.id.modify_record_user_name);
        flower_image_iv = (ImageView)findViewById(R.id.modify_record_flower_image);
        record_number_et = (EditText)findViewById(R.id.modify_record_number);
        record_money_et = (EditText)findViewById(R.id.modify_record_money);

        // set view
        time_tv.setText(TimeUtil.timestamp_to_timeString(record.getTimestamp()));
        flower_name_tv.setText(flower.getName());
        user_name_tv.setText(user.getName());
        flower_image_iv.setImageResource(flower.getImage());
        record_number_et.setText(record.getNum()+"");
        record_money_et.setText(record.getMoney()+"");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED);
                finish();
            }
        });
        flower_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RecordInfoActivity.this, RecordSelectFlower.class);
                startActivityForResult(intent, 1);
            }
        });
        submit_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String numberString = record_number_et.getText().toString();
                String moneyString = record_money_et.getText().toString();
                if(numberString.equals("")||moneyString.equals("")){
                    Toast.makeText(RecordInfoActivity.this, "数量/金额不能为空", Toast.LENGTH_SHORT).show();;
                }else{
                    int number = Integer.parseInt(numberString);
                    double money = Double.parseDouble(moneyString);
                    if(number>0 && money>0){
                        long flower_id = flower.getFlower_id();
                        long user_id = user.getUser_id();
                        Record record = new Record(timestamp, number, money, flower_id, user_id);
                        record.updateAll("timestamp==?", timestamp+"");
                        Toast.makeText(RecordInfoActivity.this, "修改成功", Toast.LENGTH_SHORT).show();;
                        setResult(RESULT_OK);
                        finish();
                    }else {
                        Toast.makeText(RecordInfoActivity.this, "数量/金额应大于0", Toast.LENGTH_SHORT).show();;
                    }
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1:
                if(resultCode==RESULT_OK){
                    long flower_id = data.getLongExtra("flower_id", 0);
                    flower = DBFindUtil.findFlowerByFlowerId(flower_id);
                    flower_image_iv.setImageResource(flower.getImage());
                    flower_name_tv.setText(flower.getName());
                }
        }
    }
}